<?php
namespace app\serverqrcode\logic;

use app\serveruser\logic\User;
use think\Db;
use \app\manage\logic\BaseLogic;
use \think\Loader;
/**
 * Created by PhpStorm.
 * User: 97606
 * Date: 2017/6/16
 * Time: 17:36
 */

class QrCode extends BaseLogic
{
    protected $name = 'qrcode';
    protected $config = 'db_config_qrcode';
    protected $classifyModel = 'app\serverqrcode\model\QrCode';

    //生成二维码
    public function createQrCode($data)
    {
        Loader::import('phpqrcode.phpqrcode');
        $dataFlow = \QRcode::png($data['url'],false,'L',$data['size'],2);
        $data['data_flow'] = base64_encode($dataFlow);
        unset($data['size']);

        $rst = $this->model($this->classifyModel)->save($data);

        if ($rst === false) {
            return auto_failure (['status' => false],'二维码生成失败！');
        } elseif ($rst === 0) {
            return auto_failure ($data,'二维码未生成！');
        }
        return auto_return(['status' => true],'二维码生成成功！');
    }

    //获取二维码
    public function gainQrCode($data)
    {
        $res = $this->db()
                    ->where($data)
                    ->find();

        if ($res === null) {
            return auto_failure ($data,'二维码数据未找到！');
        } elseif ($res === false) {
            return auto_failure ($data,'数据查询出错！');
        }

        return auto_return(['url' => $res['url'], 'qrcode' => $res['data_flow']],'二维码获取成功！');
    }
}